/**
 * Project: Useful Programs
 * Package: mathematics
 */
package org.leumasjaffe.math;

import org.eclipse.jdt.annotation.NonNull;

/**
 * 
 * Class ArrayMath in file ArrayMath.java
 * 
 * @author leumasjaffe
 * Created on: Jul 26, 2012
 *
 */
public class ArrayMath {
	
	/**
	 * @param array
	 * @return
	 */
	public static int sum(@NonNull int[] array) {
		int result = 0;
		for (int i = 0; i < array.length; i++)
			result += array[i];
		return result;
	}
	
	/**
	 * @param array
	 * @return
	 */
	public static double sum(@NonNull double[] array) {
		double result = 0;
		for (int i = 0; i < array.length; i++)
			result += array[i];
		return result;
	}
	
	/**
	 * @param array
	 * @return
	 */
	public static int product(@NonNull int[] array) {
		int result = 0;
		for (int i = 0; i < array.length; i++)
			result *= array[i];
		return result;
	}
	
	/**
	 * @param array
	 * @return
	 */
	public static double product(@NonNull double[] array) {
		double result = 0;
		for (int i = 0; i < array.length; i++)
			result *= array[i];
		return result;
	}

	/**
	 * @param array
	 * @return
	 */
	public static @NonNull String script_rep(@NonNull int[] array) {
		int size = array.length;
		if (size == 0)
			return "{}";
		String result = "{" + array[0];
		for (int i = 0; i < size; i++)
			result += ", " + array[i];
		result += "}";
		return result;
	}
	
	/**
	 * @param array
	 * @return
	 */
	public static @NonNull String script_rep(@NonNull double[] array) {
		int size = array.length;
		if (size == 0)
			return "{}";
		String result = "{" + array[0];
		for (int i = 0; i < size; i++)
			result += ", " + array[i];
		result += "}";
		return result;
	}

	/**
	 * @param array
	 * @return
	 */
	public static int maxIndex(@NonNull int[] array) {
		int size = array.length;
		if (size == 0)
			return -1;
		int i = 0, n = array[0];
		for (int j = i+1; j < size; j++)
			if (array[j] > n)
				n = array[(i = j)];
		return i;
	}
	
	/**
	 * @param array
	 * @return
	 */
	public static int maxIndex(@NonNull double[] array) {
		int size = array.length;
		if (size == 0)
			return -1;
		int i = 0;
		double n = array[0];
		for (int j = i+1; j < size; j++)
			if (array[j] > n)
				n = array[(i = j)];
		return i;
	}
	
	/**
	 * @param array
	 * @return
	 */
	public static int minIndex(@NonNull int[] array) {
		int size = array.length;
		if (size == 0)
			return -1;
		int i = 0, n = array[0];
		for (int j = i+1; j < size; j++)
			if (array[j] < n)
				n = array[(i = j)];
		return i;
	}
	
	/**
	 * @param array
	 * @return
	 */
	public static int minIndex(@NonNull double[] array) {
		int size = array.length;
		if (size == 0)
			return -1;
		int i = 0;
		double n = array[0];
		for (int j = i+1; j < size; j++)
			if (array[j] < n)
				n = array[(i = j)];
		return i;
	}

}
